<?php
	abstract class SDBModel
	{
		protected $_id;
		protected $_attributes;
		protected $_domain;
		protected $_saved;

		protected $_sdb;
		protected $_config;
		protected $_logger;
		
		public function __construct()
		{
			$this->_domain = null;
			$this->_saved = false;
	
			$this->_sdb = Zend_Registry::get('sdb'); 
			$this->_config = Zend_Registry::get('config');
			$this->_logger = Zend_Registry::get('logger'); 
		}
		
		public function __destruct()
		{
			unset($this->_domain);
			unset($this->_saved);
			unset($this->_sdb);
			unset($this->_config);
			unset($this->_logger);
		}
		
		public function setDomain($domain)
		{
			$this->_domain = $domain;
		}

		public function getDomain()
		{
			return $this->_domain;
		}

		// throws db exception
		public function load($id)
		{
			$this->_id = $id;
			$response = $this->_sdb->getAttributes($this->_domain, $this->_id); 

			if (!$response->isSetGetAttributesResult()) 
			{
				// throw exception
				$msg = "GetAttributes request failed on item {$this->_id}.";
				$this->_logger->log('(' . get_class($this) . ') ' . $msg, Zend_Log::ERR);
				$this->_logger->log('(' . get_class($this) . ') Data dump to follow...', Zend_Log::ERR);
				$this->_logger->log('(' . get_class($this) . ') id = ' . $this->_id, Zend_Log::ERR);
				$this->_logger->log('(' . get_class($this) . ') domain = ' . $this->_domain, Zend_Log::ERR);
				$this->_logger->log('(' . get_class($this) . ') ' . print_r($this->_attributes, true), Zend_Log::ERR); 
				throw new SDBModelException($msg, 0);
			}

			$getAttributesResult = $response->getGetAttributesResult();
			$attributes = $getAttributesResult->getAttribute();
				
			foreach ($attributes as $attribute)
				$this->_attributes[$attribute->getName()] = $attribute->getValue();
			
			$this->_saved = true;
		}
		
		// throws db exception
		public function save($i = 0)
		{
			if (isset($this->_id))
			{
//try { var_dump($this->_domain); var_dump($this->_id); var_dump($this->_attributes); var_dump($this->_sdb);
				$response = $this->_sdb->putAttributes($this->_domain, 
				$this->_id, $this->_attributes);
//} catch (exception $e) { var_dump($e); die(); }
			}
			else
			{
				$this->_id = ($i > 0) ? CommonGenerator::generateUniqueId($i) 
					: CommonGenerator::generateUniqueId();
				$response = $this->_sdb->putAttributes($this->_domain, 
					$this->_id, $this->_attributes);
			}

			$this->_saved = true;
		}
		
		public function delete()
		{
			if (isset($this->_id))
			{
				$response = $this->_sdb->deleteAttributes($this->_domain, 
					$this->_id);
			}

			$this->clearObject();
			$this->_saved = false;
		}
		
		public function getId()
		{
			return $this->_id;
		}

		public function setAttributes($attributes_hash)
		{
			foreach ($attributes_hash as $key => $value)
				$this->_attributes[$key] = $value;

			$this->_saved = false;
		}

		public function setAttribute($attribute_name, $attribute_value)
		{
			$this->_attributes[$attribute_name] = $attribute_value;
			$this->_saved = false;
		}

		public function getAttribute($attribute_name)
		{
			return $this->_attributes[$attribute_name];
		}

		public function getAttributes()
		{
			return $this->_attributes;
		}

		public function isSaved()
		{
			return $this->_saved;
		}

		public function clearObject()
		{
			$this->_id = null;
			$this->_attributes = array();
			$this->_saved = false;
		}
		
	}
